iT邦幫忙

2

【C#】常見的程式面試考題

c#
  • 分享至 

  • xImage
  •  

廢話不多說,程式實務直接開幹!


學習目標: 程式面試的實務

學習難度: ☆☆☆


Binary Search

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int BinarySearch(int[] array,int target)
        {
            int min = 0;

            int max = array.Length - 1;

            while(min<=max)
            {
                int mid = (min + max) / 2;

                if (target == array[mid]) return mid;

                if (target < array[mid]) max = mid - 1;

                if (target > array[mid]) min = mid + 1;
            }

            return -1;
        }

        static void Main(string[] args)
        {
            int[] array = { 1, 3, 5, 7, 9 };

            Console.WriteLine(BinarySearch(array, 9));
        }
    }
}

Fibonacci

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int Fibonacci(int num)
        {
            if (num == 0) return 0;

            if (num == 1) return 1;

            return Fibonacci(num-1)+Fibonacci(num-2);
        }

        static void Main(string[] args)
        {
            Console.WriteLine(Fibonacci(13));
        }
    }
}

DownStairs

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        public static long DownStairs(int n)
        {
            if(n==1)
            {
                return 1;
            }
            else if(n==2)
            {
                return 2;
            }
            else
            {
                return DownStairs(n - 2) + DownStairs(n - 1);
            }
        }

        public static void Main(string[] args)
        {
            Console.WriteLine(DownStairs(4));
        }
    }
}

Maximum Subarray

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        public static int GetMax(int[] array)
        {
            int sum = 0;

            int max = array[0];

            for (int i = 0; i < array.Length; ++i)
            {
                sum += array[i]; //計算陣列中連續數的總和(0~4 ~ 4~4)

                sum = Math.Max(0, sum); //排除掉負數的加總

                max = Math.Max(sum, max); //判斷當前與上一個加總哪個較大
            }
            return max;
        }

        static void Main(string[] args)
        {
            int[] array = {1,7,-70,30,30};

            Console.WriteLine(GetMax(array));
        }
    }
}

Bubble Sort

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        public static int[] BubbleSort(int[] array)
        {
            int temp;

            for (int i = 0; i < array.Length; ++i)
            {
                for (int j = i+1; j < array.Length; ++j)
                {
                    if(array[i]>array[j])
                    {                     
                        temp = array[j];

                        array[i] = array[j];

                        array[j] = temp;
                    }
                }              
            }
            return array;
        }

        static void Main(string[] args)
        {
            int[] array = {3,1,7,2,4,9};

            array = BubbleSort(array);

            foreach (var item in array)
            {
                Console.WriteLine(item+"\n");
            }
        }
    }
}

Swap two variables without temp

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static void Main(string[] args)
        {
            int a = 5;

            int b = 10;
            
            //XOR做運算

            a = a ^ b;

            b = a ^ b;

            a = a ^ b;

            Console.WriteLine("a"+a+","+"b"+b);
        }
    }
}

sum the Integers (1 to N)

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int Sum(int last)
        {
            int first = 1;

            return  (first+last)*last/ 2;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(Sum(100));
        }
    }
}

sum the Integers (1x2+2x3+3x4+...(n-1)xn)

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int Sum(int n)
        {
            int sum=0;

            for (int i = 2; i < n; i++)
            {
                sum += (i - 1) * i;
            }

            return sum;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(Sum(100));
        }
    }
}

sum the Integers (1X1+2X2+3X3+...+nxn)

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int Sum(int n)
        {
            int sum=0;

            for (int i = 1; i <= n; i++)
            {
                sum += i * i;
            }

            return sum;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(Sum(100));
        }
    }
}

Find a max number in unsort array

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int FindMax(int[] array)
        {
            int max = 0;

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] > max) max = array[i];
            }

            return max;
        }

        static void Main(string[] args)
        {
            int[] array = new int[] { 1, 5, 7, 11, 2, 4, 9, 20, 23, 8, 13, 30, 6 };

            Console.WriteLine(FindMax(array));
        }
    }
}

Find a second max number in unsort array

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int FindSecondMax(int[] array)
        {
            int max= 0;

            int second = 0;

            for (int i = 0; i < array.Length; i++)
            {

                int temp = array[i];

                if (temp > max)
                {
                    second = max; max = temp; 
                }

                else if (temp > second) second = temp;

            }

            return second;
        }

        static void Main(string[] args)
        {
            int[] array = new int[] { 1, 5, 7, 11, 2, 4, 9, 20, 23, 8, 13, 30, 6 };

            Console.WriteLine(FindSecondMax(array));
        }
    }
}

Find the missing integer in array

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int Sum(int[] array)
        {
            int sum=0;

            for (int i = 0; i < array.Length; i++)
            {
                sum += array[i];
            }

            return sum;
        }

        static void Main(string[] args)
        {
            int[] array = new int[] { 1,2,3,4,6,7,8 };

            int result = ((array[0] + array[array.Length-1]) * (array.Length + 1) / 2) - Sum(array);

            Console.WriteLine(result);
        }
    }
}

Find the larger number without judgement statements

using System;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int max(int a, int b)
        {
            return ((a + b) + Math.Abs((a - b))) / 2;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(max(1, 3));
        }
    }
}

參考資料:

https://emn178.pixnet.net/blog/post/92389195


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言